package com.jintian.smart.kernel.security.handler;

import com.jintian.smart.base.api.security.entity.HeartBeatRecordEntity;
import com.jintian.smart.kernel.core.support.IHeartBeatRecordHandler;
import com.jintian.smart.kernel.security.mapper.HeartBeatRecordMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import com.jintian.smart.kernel.core.support.HeartBeatRecord;
import com.jintian.smart.kernel.common.util.SmartBeanUtil;
import org.springframework.stereotype.Service;

/**
 * 心跳记录
 */
@Slf4j
@Service
public class HeartBeatRecordHandler implements IHeartBeatRecordHandler {

    @Resource
    private HeartBeatRecordMapper heartBeatRecordMapper;

    /**
     * 心跳日志处理方法
     *
     * @param heartBeatRecord
     */
    @Override
    public void handler(HeartBeatRecord heartBeatRecord) {
        HeartBeatRecordEntity heartBeatRecordEntity = SmartBeanUtil.copy(heartBeatRecord, HeartBeatRecordEntity.class);
        HeartBeatRecordEntity heartBeatRecordOld = heartBeatRecordMapper.query(heartBeatRecordEntity);
        if (heartBeatRecordOld == null) {
            heartBeatRecordMapper.insert(heartBeatRecordEntity);
        } else {
            heartBeatRecordMapper.updateHeartBeatTimeById(heartBeatRecordOld.getHeartBeatRecordId(), heartBeatRecordEntity.getHeartBeatTime());
        }
    }
}
